﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// The class PolynomialComponent represents a part of a polynomial.
    /// </summary>
    [Serializable]
    public class PolynomialComponent : IPolynomialComponent
    {
        /// <summary>
        /// The coefficient of the PolynomialComponent.
        /// </summary>
        private double coefficient;

        /// <summary>
        /// The exponent of the PolynomialComponent.
        /// </summary>
        private double exponent;

        /// <summary>
        /// Initializes a new instance of the <see cref="PolynomialComponent"/> class.
        /// </summary>
        public PolynomialComponent()
        {
            this.coefficient = 1;
            this.exponent = 1;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PolynomialComponent"/> class.
        /// </summary>
        /// <param name="component">The component to clone.</param>
        public PolynomialComponent(PolynomialComponent component)
        {
            if (component == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("component");
            }

            this.coefficient = component.Coefficient;
            this.exponent = component.Exponent;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PolynomialComponent"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the PolynomialComponent.</param>
        public PolynomialComponent(double coefficient)
        {
            this.coefficient = coefficient;
            this.exponent = 1;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PolynomialComponent"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the PolynomialComponent.</param>
        /// <param name="exponent">The exponent of the PolynomialComponent.</param>
        public PolynomialComponent(double coefficient, double exponent)
        {
            this.coefficient = coefficient;
            this.exponent = exponent;
        }

        /// <summary>
        /// Gets or sets the coefficient of the PolynomialComponent.
        /// </summary>
        /// <value>The coefficient of the PolynomialComponent.</value>
        public double Coefficient
        {
            get { return coefficient; }
            set { coefficient = value; }
        }

        /// <summary>
        /// Gets or sets the exponent of the PolynomialComponent.
        /// </summary>
        /// <value>The exponent of the PolynomialComponent.</value>
        public double Exponent
        {
            get { return exponent; }
            set { exponent = value; }
        }

        /// <summary>
        /// Inverts the value of the coefficient.
        /// </summary>
        public void InvertCoefficient()
        {
            this.coefficient = -this.coefficient;
        }

        /// <summary>
        /// Inverts the value of the exponent.
        /// </summary>
        public void InvertExponent()
        {
            this.exponent = -this.exponent;
        }

        /// <summary>
        /// Adds a coefficient value from another PolynomialComponent to the current coefficient.
        /// </summary>
        /// <param name="add">The component to add.</param>
        public void AddCoefficient(PolynomialComponent add)
        {
            if (add == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("add");
            }

            this.coefficient += add.Coefficient;
        }

        /// <summary>
        /// Calculates the value of the current component with a specified value for x.
        /// </summary>
        /// <param name="x">The value for x.</param>
        /// <returns>The value of the current component with the specified value for x.</returns>
        public double CalulateValue(double x)
        {
            if ((this.exponent < 0) && (this.coefficient == 0))
            {
                return 0;
            }

            return this.coefficient * Math.Pow(x, this.exponent);
        }

        /// <summary>
        /// Derivatives the current component.
        /// </summary>
        /// <returns>The derivative of the current component.</returns>
        public PolynomialComponent Derivative()
        {
            return new PolynomialComponent(this.coefficient * this.exponent, this.exponent - 1);
        }

        /// <summary>
        /// Copies the current instance of the component.
        /// </summary>
        /// <returns>The current instance of the component.</returns>
        public PolynomialComponent Copy()
        {
            return new PolynomialComponent(this.coefficient, this.exponent);
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first PolynomialComponent.</param>
        /// <param name="b">The second PolynomialComponent.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(PolynomialComponent a, PolynomialComponent b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.Coefficient == b.Coefficient) && (a.Exponent == b.Exponent));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first PolynomialComponent.</param>
        /// <param name="b">The second PolynomialComponent.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(PolynomialComponent a, PolynomialComponent b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first PolynomialComponent.</param>
        /// <param name="b">The second PolynomialComponent.</param>
        /// <returns>The result of the operator.</returns>
        public static PolynomialComponent operator +(PolynomialComponent a, PolynomialComponent b)
        {
            if (a == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Exponent != b.Exponent)
            {
                throw new IllegalArithmeticException("For a addition the exponents have to be even.");
            }

            return new PolynomialComponent(a.Coefficient + b.Coefficient, a.exponent);
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first PolynomialComponent.</param>
        /// <param name="b">The second PolynomialComponent.</param>
        /// <returns>The result of the operator.</returns>
        public static PolynomialComponent operator -(PolynomialComponent a, PolynomialComponent b)
        {
            if (a == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Exponent != b.Exponent)
            {
                throw new IllegalArithmeticException("For a subtraction the exponents have to be even.");
            }

            return new PolynomialComponent(a.Coefficient - b.Coefficient, a.exponent);
        }

        /// <summary>
        /// Implements the operator *.
        /// </summary>
        /// <param name="a">The first PolynomialComponent.</param>
        /// <param name="b">The second PolynomialComponent.</param>
        /// <returns>The result of the operator.</returns>
        public static PolynomialComponent operator *(PolynomialComponent a, PolynomialComponent b)
        {
            if (a == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("b");
            }

            return new PolynomialComponent(a.Coefficient * b.Coefficient, a.exponent + b.exponent);
        }

        /// <summary>
        /// Compares the current component with another one.
        /// </summary>
        /// <param name="compare">The component to compare.</param>
        /// <returns>True if the components are even otherwise, false.</returns>
        public bool CompareTo(PolynomialComponent compare)
        {
            if (compare == (PolynomialComponent) null)
            {
                throw new ArgumentNullException("compare");
            }

            return ((this.coefficient == compare.Coefficient) && (this.exponent == compare.Exponent));
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.coefficient + "x^" + this.exponent;
        }
    }
}